//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		action:'manage.good_list/',
		pagetitle:'商品管理',
		loading:true,
		page:1,
		keyword:''
	},
	onReady: function (){
		//获得粉丝消息弹窗(需要时开启)
		//this.ShowAuthM = this.selectComponent("#ShowAuthM");
		var fansinfo = wx.getStorageSync('userInfo');
		if(!fansinfo){
			//this.ShowAuthM.show();
		}
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		app.util.get(app.access,function(pagedata){
			console.log('pagedata');console.log(pagedata);
			page.setData({
				pagedata:pagedata,
				action:app.access.action
			});
			page.getlist(1)
		});
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		app.util.getUserInfo(e);
		this.ShowAuthM.hide();
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
	chooseImage:function(e){
		var the = this
		var pagedata = the.data.pagedata
		app.util.image(function(res){
			the.setData({
				'pagedata.item.image':res
			})
		})
	},
	keyword:function(){
		var the = this
		var pagedata = the.data.pagedata
		pagedata.params.keyword = e.detail.value
	},
	showtotal:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data=e.currentTarget.dataset;
		pagedata.g=pagedata.list[data.index]
		the.setData({
			'pagedata.showtotal':true,
			'pagedata.g':pagedata.g,
			index:data.index
		})
	},
	settotal:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var index=the.data.index
		app.util.post('jy_weishop/store.manage.goods/settotal',pagedata.g,function(res){
			pagedata.list[index].total=pagedata.g.total;
			the.setData({
				'pagedata.showtotal':false,
				'pagedata.list':pagedata.list,
			})
		});
	},
	uptotal:function(e){
		var the=this
		var data=e.detail.value;
		the.setData({
				'pagedata.g.total':data
		});
	},
	getlist:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var loading = the.data.loading
		var page = the.data.page
		var keyword = the.data.keyword
		if(e==1){
			page = 1;
			pagedata.list=[]
		}
		if(pagedata.params){
			if(pagedata.params.keyword){
				console.log('pagedata.params.keyword');
				console.log(pagedata.params.keyword);
				keyword =pagedata.params.keyword;
			}else{
				keyword ='';
			}
		}else{
			keyword ='';
		}		
		var access = 'jy_weishop/store.manage.goods/getlist/page='+page+'&keyword='+keyword
		app.util.get(access,function(res){
			if(res.error !=1){
				for (var i=0;i<res.length;i++){
					pagedata.list.push(res[i]);
				}
				page++;
				loading = false;
				the.setData({
					page:page,
					loading:loading,
					'pagedata.list':pagedata.list
				})
			}else{
				onsole.log('error');
			};
		})
	},
	//翻页
	onReachBottom: function () {
		var isnodata=this.data.isnodata
		var loading=this.data.loading
		if(!isnodata && !loading){
			this.getlist()
		}
	},
});